<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>合并区间</title>
</head>
<body>
  <script>
    function fn(intervals) {
      let res = [];
      intervals.sort((a, b) => a[0] - b[0]);
      let prev = intervals[0];
      for (let i = 1; i < intervals.length; i++) {
        let cur = intervals[i];
        if (prev[1] >= cur[0]) { // 有重合
          prev[1] = Math.max(cur[1], prev[1]);
        } else {       // 不重合，prev推入res数组 
          res.push(prev);
          prev = cur;  // 更新 prev
        }
      }
      res.push(prev)
      return res
    }
    console.log(fn([[1, 4], [4, 5]]))
  </script>
</body>
</html>